package com.ecustbookstore.mymall.common;

import lombok.Data;

/**
 * @description:通用的返回对象
 * @author: Tian
 * @time: 2021/6/2 22:01
 */

@Data
public class CommonResponse<T> {

    /**
     * 状态码
     */
    private long code;
    /**
     * 返回的信息提示
     */
    private String message;
    /**
     * 对数剧的封装
     */
    private T data;

    /**
     * 使用单例模式
     */
    private CommonResponse() {

    }

    private CommonResponse(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }


    public  boolean isSuccess(){
        return this.code == ResponseCode.SUCCESS.getCode();
    }
    /**
     * 成功返回结果
     */
    public static <T> CommonResponse<T> success(T data) {
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage(), data);
    }

    /**
     * 成功返回结果
     */
    public static <T> CommonResponse<T> success(String message, T data) {
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败返回结果
     */
    public static <T> CommonResponse<T> failed() {
        return new CommonResponse<T>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getMessage(), null);
    }
    /**
     * 失败返回结果
     */
    public static <T> CommonResponse<T> failed(String message) {
        return new CommonResponse<T>(ResponseCode.ERROR.getCode(), message, null);
    }


}
